import java.text.SimpleDateFormat
import java.util.*

/**
 * 时间工具类
 */
object NumberFormatUtil {

    private val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS")

    fun timeToLong(time: String): Long {
        var date: Date? = null
        try {
            date = simpleDateFormat.parse(time)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return date?.time ?: 0
    }

    fun timeToString(date: Date): String {
        return simpleDateFormat.format(date)
    }

    fun longToString(time: Long): String {
        return simpleDateFormat.format(Date(time));
    }

    fun formatDuring(mss: Long): String {
        val days = mss / (1000 * 60 * 60 * 24)
        val hours = mss % (1000 * 60 * 60 * 24) / (1000 * 60 * 60)
        val minutes = mss % (1000 * 60 * 60) / (1000 * 60)
        val seconds = mss % (1000 * 60) / 1000
        val d = if (days > 0) "${days}天" else ""
        val h = if (hours > 0) "${hours}小时" else ""
        val m = if (minutes > 0) "${minutes}分钟" else ""
        val s = if (seconds > 0) "${seconds}秒" else ""
        return "$d$h$m$s"
    }
}
